package com.mvetc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mvetc.entity.Movies;
import com.mvetc.entity.PageInfo;
import com.mvetc.service.MoviesService;
import com.mvetc.service.impl.MoviesServiceimpl;

/**
 * Servlet implementation class MoviesSearch
 */
@WebServlet("/MoviesSearch")
public class MoviesSearch extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MoviesService moviesServiceImpl = new MoviesServiceimpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoviesSearch() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
			if ("pageInfoByMoviesType".equals(op)) {
				DoSearchMoviesByType(request,response);
			}else if ("searchMovieByArea".equals(op)) {
				DoSearchMoviesByArea(request,response);
			}else if ("searchMoviesByQualityChoice".equals(op)) {
				DoSearchMoviesByQualityChoice(request,response);
			}
		
	}
	private void DoSearchMoviesByQualityChoice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> params=new HashMap<String, String>();
		String score = request.getParameter("score");//评分
		String clickCount = request.getParameter("clickCount");//点击量
		String publickDate = request.getParameter("publickDate");//发布时间
		params.put("score", score);
		params.put("clickCount", clickCount);
		params.put("publickDate", publickDate);
		List<Movies> listMovies = moviesServiceImpl.getListMoviesByQualityChoiceBycf(params);
		response.setContentType("application/json;charset=UTF-8");
		Gson gson=new Gson();
		String listjson = gson.toJson(listMovies);
		response.getWriter().write(listjson);
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 * @author 陈奋
	 * @time 2020-06-16
	 * 通过地区搜索电影
	 */
	private void DoSearchMoviesByArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> params=new HashMap<String, String>();
		String areaName = request.getParameter("areaName");//获取前端携带的Message的参数
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		params.put("areaName", areaName);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		PageInfo<Movies> list = moviesServiceImpl.getListMoviesByConditionBycf(params);	
		response.setContentType("application/json;charset=UTF-8");
		Gson gson=new Gson();
		String listjson = gson.toJson(list);
		response.getWriter().write(listjson);
		
	}

	private void DoSearchMoviesByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> params=new HashMap<String, String>();
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String typeName = request.getParameter("typeName");//获取前端携带的typeName的参数
		params.put("typeName", typeName);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		PageInfo<Movies> list = moviesServiceImpl.getListMoviesByConditionBycf(params);
		response.setContentType("application/json;charset=UTF-8");
		Gson gson=new Gson();
		String listjson = gson.toJson(list);
		response.getWriter().write(listjson);
	}
	
}
